# -*- coding: utf-8 -*-
# @Time    : 2022/4/11 16:38
# @Author  : zyw
# @Email   : panda_kings@163.com
# @File    : channel.py
import json
from flask import Blueprint,g,jsonify,request
from flask_restful import Api,Resource,reqparse

from common.utils.login_util import login_required
from common.models.users import User
# from common.models.channeles import Channels,UserChannel
from common.models import db

user_channel_bp = Blueprint('channel',__name__, url_prefix='/channels')

api = Api(user_channel_bp)


# class ChannelRes(Resource):
#     """用户订阅频道: 用户必须先登录"""
#     # method_decorators = [login_required]
#
#     def get(self):
#         """获取用户订阅的频道"""
#         parse = reqparse.RequestParser()
#         parse.add_argument('uid')
#         args = parse.parse_args()
#         uid = args.get('uid')
#         print('uid',uid)
#         # 获取用户订阅频道信息
#         user_channel = UserChannel.query.filter_by(uid=int(uid)).all()
#         print('channels>>',user_channel)
#         # 根据用户频道,获取频道信息
#         channels = []
#         for i in user_channel:
#             print('i>>',i.cid)
#             channel= Channels.query.filter_by(cid=i.cid).first()
#             channels.append({
#                 'id':channel.cid,
#                 "name":channel.name
#             })
#
#         return jsonify({'msg':"ok","data":channels,"uid":uid})
#
#     def post(self):
#         """用户添加频道"""
#         # 获取前端传来的频道id
#         parse = reqparse.RequestParser()
#         parse.add_argument('cid',type=str, location='json') # cid 可以传来多个频道id [1,2,3,4]
#         parse.add_argument('uid')
#         args = parse.parse_args()
#         # 使用request 接收列表
#         cid = args['cid'] # 此时cid 是str: '[1,2,3,4]'
#         uid = args.get('uid')
#         # print("参数的值>>",cid,uid, type(cid))
#
#         user = User.query.get(int(uid))
#         if not user:
#             print("获取用户信息错误")
#
#         for id in eval(cid):
#             # 获取的是每一个cid
#             channel = Channels.query.filter_by(cid=id).first()
#             if channel:
#                 user_channel = UserChannel(
#                     uid=user.uid,
#                     cid=id
#                 )
#                 db.session.add(user_channel)
#                 db.session.commit()
#             else:
#                 return jsonify({'msg':"此频道不存在","code":400})
#
#         return jsonify({"msg":"订阅频道ok","code":200})
#
#     def delete(self):
#         """删除用户频道"""
#         parse = reqparse.RequestParser()
#         parse.add_argument('uid')
#         parse.add_argument('cid', type=str, location='args')
#         args = parse.parse_args()
#         uid = args.get('uid')
#         cid = args.get('cid')
#         # 获取用户订阅频道信息
#         user_channel = UserChannel.query.filter_by(uid=int(uid)).all()
#         print("用户订阅的频道", user_channel, type(user_channel))
#
#         user_cid = [channel.cid for channel in user_channel]
#         print(user_cid)
#         for id in eval(cid):
#             if id in user_cid:
#                 user_cid.remove(id)
#         data = []
#         for cid in user_cid:
#             channel = Channels.query.get(cid)
#             data.append({
#                 'id':channel.cid,
#                 "name":channel.name
#             })
#
#         return jsonify({"msg":"ok","data":data})


class UserChannelNews(Resource):
    method_decorators = [login_required]
    def get(self):
        """用户关注频道的文章"""
        # 1. 获取频道id
        # 2. 根据频道id,去获取文章
        # 3. 返回
        """
        {
            "msg":'ok',
            "data":[
                {
                    "cid":1,
                    "news":[
                        {
                            "nid":1,
                            "title":"Numpy 处理时间序列",
                            "like_count":100
                        },
                        ....
                    ]
                },
                {
                    "cid":2,
                    "news":[
                        {
                            "nid":3,
                            "title":"HDFS",
                            "like_count":100
                        },
                        ....
                    ]
                }
            .....
            ]
        }
        """
        pass


# 注册路由
# api.add_resource(ChannelRes,'/channels')














